package com.gujia.rabbitmq.ps;

import com.gujia.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class PsConsumer {

    private static final String EXCHANGE_NAME="test_exchange_fanout";
    private static final String QUEUE_NAME="test_queue_fanout";
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtils.getConnction();

        Channel channel = connection.createChannel();
        //声明队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //绑定队列到交换机 转发器
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"");
        channel.basicQos(1);

        Consumer consumer = new DefaultConsumer(channel){
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope,
                                               AMQP.BasicProperties properties, byte[] body) throws IOException {
                        String msg = new String(body,"utf-8");
                        System.out.println("psConsumer:"+msg);
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }finally {
                            System.out.println("psConsumer done！");
                            channel.basicAck(envelope.getDeliveryTag(),false);
                        }
            }
        };
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME,autoAck,consumer);
    }
}
